package cn.xiaoyu.android.activity.index.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import cn.xiaoyu.android.R;
import cn.xiaoyu.android.activity.index.entity.ItemEntity;

/**
 * Author: fengzhe
 * Date: 2019/3/15
 * Email: fengzhe@sis.sh.cn
 * Description:
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.VH> {

    public class VH extends RecyclerView.ViewHolder {
        public final TextView title;

        VH(View v) {
            super(v);
            title = v.findViewById(R.id.tv_function_label);
        }
    }

    public interface OnItemClickedListener {
        void onItemClicked(int position);
    }

    private OnItemClickedListener onItemClickedListener;

    private List<ItemEntity> data;

    public RecyclerViewAdapter(List<ItemEntity> data, OnItemClickedListener onItemClickedListener) {
        this.data = data;
        this.onItemClickedListener = onItemClickedListener;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_launcher, viewGroup, false);
        return new VH(v);
    }

    @Override
    public void onBindViewHolder(@NonNull final VH holder, int position) {
        holder.title.setText(data.get(position).getLabel());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickedListener.onItemClicked(holder.getAdapterPosition());
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

}